/**
 * 
 */
package com.maywide.jdp.system.aspect;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.servlet.ViewAttribute;
import com.maywide.jdp.system.spring.JSONView;

/**
 * @author luoyang
 */
@Aspect
@Component("jsonAspect")
public class JSONAspect {
	@SuppressWarnings( { "unused" })
	@Around(value="execution(public * com.maywide.jdp.system.service.*ServiceImpl.*(..)) and args(..,request,response,session) and @annotation(json)",argNames="json,request,response,session")
	private Object doJsonResponse(ProceedingJoinPoint jp,JSON json,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Throwable {
		Object retVal = jp.proceed();
		if(json.value()) {
			request.setAttribute(ViewAttribute.VIEW_BEAN_NAME, JSONView.VIEW_NAME);
			if(retVal instanceof Map)
				request.setAttribute("isMapObject", true);
		}
		return retVal;
	}
}
